package selfview.sufutian.com.myapplication.util;


import android.os.Handler;
import android.os.Message;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;


/**
 * Create by SuFuTian on 2018/12/27
 * Email: 1066899696@qq.com
 * <p>
 * _______________*********_______________________
 * ______________************_____________________
 * ______________*************____________________
 * _____________**__***********___________________
 * ____________***__******_*****__________________
 * ____________***_*******___****_________________
 * ___________***__**********_****________________
 * __________****__***********_****_______________
 * ________*****___***********__*****_____________
 * _______******___***_********___*****___________
 * _______*****___***___********___******_________
 * ______******___***__***********___******_______
 * _____******___****_**************__******______
 * ____*******__*********************_*******_____
 * ____*******__******************************____
 * ___*******__******_*****************_*******___
 * ___*******__******_******_*********___******___
 * ___*******____**__******___******_____******___
 * ___*******________******____*****_____*****____
 * ____******________*****_____*****_____****_____
 * _____*****________****______*****_____***______
 * ______*****______****________***______*________
 * ________**_______****________****______________
 */
public class ThreadDemo {

    private static ScheduledExecutorService scheduledThreadPool;


    private static int timeCount;

    /**
     *
     */
    public static void main(String[] args) {
        scheduledThreadPool = Executors.newScheduledThreadPool(5);
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                timeCount++;
                System.out.println("scanWork: " + timeCount);
                if (timeCount < 3) {
                    searchWork();
                } else {
                    if (!scheduledThreadPool.isShutdown()) {
                        scheduledThreadPool.shutdown();
                    }
                }
            }
        };
        timer.schedule(task, 1000, 3000);
        task.run();
    }


    public static int workCount;

    /**
     * 查找任务工作
     */
    public static void searchWork() {
        for (int i = 0; i < 5; i++) {
            WorkThread thread = new WorkThread();
            workCount++;
            System.out.println("任务" + " :" + workCount);
            thread.setWorkId(workCount);
            scheduledThreadPool.execute(thread);
        }
    }


    public static class WorkThread extends Thread {

        private int workId;

        public void setWorkId(int workId) {
            this.workId = workId;
        }

        @Override
        public void run() {
            super.run();
            //5秒执行完一个任务
            String name = Thread.currentThread().getName();
            for (int i = 0; i < 50; i++) {
                System.out.println(name + " :" + workId);
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(name + " --------down :" + workId);
        }
    }


}
